package com.lfg.leetcode.leetcode1000;


import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class No1302deepestLeavesSum {
    public static void main(String[] args) {
        System.out.println(deepestLeavesSum(TreeNode.stringToTreeNode("[1,2,3,4,5,null,6,7,null,null,null,null,8]")));
    }

    public static int deepestLeavesSum(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int ret  = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            ret = 0;
            for(int i=0;i<size;i++) {
                TreeNode node = queue.poll();
                if(null == node){
                    continue;
                }
                ret += node.val;
                if(node.right!= null){
                    queue.offer(node.right);
                }
                if(node.left!= null){
                    queue.offer(node.left);
                }
            }
        }
        return ret;

    }
}

